package com.winit.openapi.ums.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.query.SearchOperator;
import com.winit.common.query.Searchable;
import com.winit.common.query.Sort;
import com.winit.common.query.filter.SearchFilterHelper;
import com.winit.common.spi.context.CommandContext;
import com.winit.common.utils.JsonMapper;
import com.winit.erp.spi.warehouse.WarehouseService;
import com.winit.erp.spi.warehouse.command.WarehouseCommand;
import com.winit.erp.spi.warehouse.vo.Warehouse;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.pms.spi.winit.LogisticsdistService;
import com.winit.pms.spi.winit.WinitPostService;
import com.winit.pms.spi.winit.WinitProductService;
import com.winit.pms.spi.winit.WinitRuleService;
import com.winit.pms.spi.winit.WinitSlaService;
import com.winit.pms.spi.winit.command.LogisticsDistFindCommand;
import com.winit.pms.spi.winit.command.WinitPostFindCommand;
import com.winit.pms.spi.winit.command.WinitProductQueryCommand;
import com.winit.pms.spi.winit.command.WinitRuleQueryCommand;
import com.winit.pms.spi.winit.command.WinitSlaFindCommand;
import com.winit.pms.spi.winit.vo.LogisticsDist;
import com.winit.pms.spi.winit.vo.WinitPost;
import com.winit.pms.spi.winit.vo.WinitProduct;
import com.winit.pms.spi.winit.vo.WinitRule;
import com.winit.pms.spi.winit.vo.WinitSla;
import com.winit.ums.spi.AddressServiceService;
import com.winit.ums.spi.CustomerAddressService;
import com.winit.ums.spi.ServiceWarehouseService;
import com.winit.ums.spi.UmsCustomerService;
import com.winit.ums.spi.address.ReceiveAddressService;
import com.winit.ums.spi.address.command.QueryProductLineAndProductCommand;
import com.winit.ums.spi.address.command.QueryReceiveAddressVerifyDetailCommand;
import com.winit.ums.spi.address.vo.AddressRulesProductLineVO;
import com.winit.ums.spi.address.vo.ReceiveAddressVerifyVo;
import com.winit.ums.spi.commnad.AddCustomerAddressCommand;
import com.winit.ums.spi.commnad.CustomerAddressQueryCommand;
import com.winit.ums.spi.commnad.DeleteCustomerAddressCommand;
import com.winit.ums.spi.commnad.QueryServiceAndWarehouseCommand;
import com.winit.ums.spi.commnad.QueryWarehouseCommand;
import com.winit.ums.spi.commnad.SetDefaultAddressCommand;
import com.winit.ums.spi.commnad.UpdateCustomerAddressCommand;
import com.winit.ums.spi.commnad.customer.address.QueryReturnAddressesCommand;
import com.winit.ums.spi.vo.CustomerAddress;
import com.winit.ums.spi.vo.CustomerWarehouse;
import com.winit.ums.spi.vo.ServiceAndWarehouse;
import com.winit.ums.spi.vo.customer.address.QueryReturnAddressesVo;

/**
 * 卖家地址管理
 * 
 * @author haiqiang.yang 2015-6-3 下午4:33:25
 * @since 1.0
 */
@Controller
@RequestMapping(value = "ums/address")
public class CustomerAddressController extends BaseController {

    @Resource
    CustomerAddressService       customerAddressService;

    @Resource
    AddressServiceService        addressServiceService;

    @Resource
    ServiceWarehouseService      serviceWarehouseService;

    @Resource
    WarehouseService             warehouseService;

    @Resource
    private WinitRuleService     winitRuleService;

    @Resource
    private WinitProductService  winitProductService;

    @Resource
    private LogisticsdistService logisticsdistService;

    @Resource
    private WinitSlaService      winitSlaService;

    @Resource
    private WinitPostService     winitPostService;

    /**
     * ums客户信息
     */
    @Resource
    private UmsCustomerService   umsCustomerService;
    
    @Resource
    private ReceiveAddressService receiveAddressService;

    @RequestMapping(value = "/add")
    public String addAddress(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object params = requestMsg.getData();
        JSONObject json = null;
        if (params instanceof JSONObject) {
            json = (JSONObject) params;
            validateParams(json);
        } else {
            return ERROR;
        }
        AddCustomerAddressCommand command = JsonMapper.fromJson(json.toString(), AddCustomerAddressCommand.class);
        command.setCtx(CommandContext.getContext());
        command.setIsPotal(false);
        //customerAddressService.addCustomerAddress(command);
        customerAddressService.addAddress(command);
        return SUCCESS;
    }

    /**
     * 修改客户地址
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     */
    @RequestMapping(value = "/update")
    public String update(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        Object params = requestMsg.getData();
        JSONObject json = null;
        if (params instanceof JSONObject) {
            json = (JSONObject) params;
            validateParams(json);
        }else{
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        UpdateCustomerAddressCommand command = JsonMapper.fromJson(json.toString(), UpdateCustomerAddressCommand.class);
        command.setCtx(CommandContext.getContext());
        command.setIsPotal(false);
        //customerAddressService.updateCustomerAddress(command);
        String isUpdate = customerAddressService.updateAddress(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("isUpdate", isUpdate);
        responseMsg.setData(data);
        return SUCCESS;
    }

    /**
     * 修改客户地址
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     */
    @RequestMapping(value = "/delete")
    public String delete(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        Object params = requestMsg.getData();
        JSONObject json = null;
        if (params instanceof JSONObject) {
            json = (JSONObject) params;
        }else{
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        DeleteCustomerAddressCommand command = JsonMapper.fromJson(json.toString(), DeleteCustomerAddressCommand.class);
        //customerAddressService.deleteCustomerAddressCommand(command);
        customerAddressService.deleteAddress(command);
        return SUCCESS;
    }

    /**
     * 查询卖家地址，分页
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/query")
    public String queryAddresses(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        CustomerAddressQueryCommand command = new CustomerAddressQueryCommand();
        Searchable<?> searchable = Searchable.newSearchable();
        searchable.addSort(new Sort(Sort.Direction.DESC, "ID"));
        command.setCtx(CommandContext.getContext());
        command.setUserId(Long.parseLong(CommandContext.getContext().getUserId()));
        Object params = requestMsg.getData();

        JSONObject json = null;
        if (params instanceof JSONObject) {
            json = (JSONObject) params;
            int pageNo = 1, pageSize = 10; // 分页参数
            if (ValidateUtil.isExistValue(json, "pageParams")) {
                JSONObject pageParam = json.getJSONObject("pageParams");
                if (ValidateUtil.isExistValue(pageParam, "pageNo")) {
                    pageNo = pageParam.getIntValue("pageNo") == 0 ? 1 : pageParam.getIntValue("pageNo");

                }
                if (ValidateUtil.isExistValue(pageParam, "pageSize")) {
                    pageSize = pageParam.getIntValue("pageSize");
                }
            }
            searchable.setPage(pageNo, pageSize);
        } else {
            throw new ApiException(ErrorCode.ILLEGAL_JSON_STRING);
        }
        searchable.and(SearchFilterHelper.newCondition("user_id", SearchOperator.eq, command.getUserId()));
        searchable.and(SearchFilterHelper.newCondition("isdelete", SearchOperator.ne, "Y"));
        command.setSearchable(searchable);
        Page<CustomerAddress> addresses = customerAddressService.queryCustomerAddress(command);
        
        List<CustomerAddress> list = addresses.content;

        if (json.containsKey("winitProductCode")) {
            String winitProductCode = json.getString("winitProductCode");
            if(list != null && isNeedSwapLanguage(winitProductCode)){
                for(CustomerAddress customerAddress:list){
                    if(ApiConstant.COUNTRY_CODE_CN.equals(customerAddress.getCountryCode())){
                        customerAddress.setStateName(customerAddress.getState());
                        customerAddress.setCityName(customerAddress.getCityCode());
                        customerAddress.setDetailCn(customerAddress.getDetailEn());
                        customerAddress.setDistrictName(customerAddress.getDistrictCode());
                        customerAddress.setContactPerson(customerAddress.getContactPersonEn());
                    }
                }
            }
        }
        
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, Object> page = new HashMap<String, Object>();
        page.put("pageNo", addresses.getPageable().getPageNumber());
        page.put("pageSize", addresses.getPageable().getPageSize());
        page.put("totalCount", addresses.total);
        data.put("list", list);
        data.put("pageParams", page);
        responseMsg.setData(data);
        return SUCCESS;
    }

    /**
     * 根据id查询卖家地址
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/get")
    public String queryAddress(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        CustomerAddressQueryCommand command = new CustomerAddressQueryCommand();
        Searchable<?> searchable = Searchable.newSearchable();
        command.setSearchable(searchable);
        command.setCtx(CommandContext.getContext());
        Object params = requestMsg.getData();

        JSONObject json = null;
        if (params instanceof JSONObject) {
            json = (JSONObject) params;
            if (ValidateUtil.isExistValue(json, "id")) {
                command.setAddressID(json.getLongValue("id"));
                searchable.and(SearchFilterHelper.newCondition("id", SearchOperator.eq, command.getAddressID()));
            } else {
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "id");
            }
            searchable.setPage(0, 10);
        } else {
            throw new ApiException(ErrorCode.ILLEGAL_JSON_STRING);
        }
        Page<CustomerAddress> addresses = customerAddressService.queryCustomerAddress(command);
        Map<String, Object> data = new HashMap<String, Object>();
        if (addresses.getContent() != null && !addresses.getContent().isEmpty()) {
            data.put("address", addresses.getContent().get(0));
        }
        responseMsg.setData(data);
        return SUCCESS;
    }

    
    /**
     * 根据id查询卖家地址
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryProductLineAndProduct")
    public String queryProductLineAndProduct(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	JSONObject json = (JSONObject) requestMsg.getData();
        long userId = json.getLong("userId");
        String code = json.getString("code");

        QueryProductLineAndProductCommand command = new QueryProductLineAndProductCommand();
        
        command.setCtx(CommandContext.getContext());
        command.setUserId(userId);
        command.setCode(code);
        List<AddressRulesProductLineVO> list = receiveAddressService.queryProductLineAndProduct(command);

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", list);
        responseMsg.setData(data);
        return SUCCESS;
    }

    /**
     * 按userID与productCode和dispatchCode查询可提货地址接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     * @author peipei.zeng
     */
    @RequestMapping(value = "/getByPickupService")
    public String getByPickupService(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        JSONObject json = (JSONObject) requestMsg.getData();
        ValidateUtil.validNotNull(json, "winitProductCode");
        ValidateUtil.validMaxLength(json, "winitProductCode", 50);
        ValidateUtil.validNotNull(json, "dispatchType");
        
        String dispatchType = json.getString("dispatchType");
        if (ApiConstant.DISPATCH_TYPE_PICK_UP.equals(dispatchType)) {
            // 老揽收地址查询接口迁移到queryReceiveAddressList
            return "/ums/receiveAddress/queryReceiveAddressList";
            
        }else{
            CustomerAddressQueryCommand command = new CustomerAddressQueryCommand();
            Searchable<?> searchable = Searchable.newSearchable();
            command.setSearchable(searchable);
            searchable.addSort(new Sort(Sort.Direction.DESC, "ID"));
            command.setCtx(CommandContext.getContext());
            command.setUserId(Long.parseLong(CommandContext.getContext().getUserId()));
            searchable.and(SearchFilterHelper.newCondition("user_id", SearchOperator.eq, command.getUserId()));
            searchable.and(SearchFilterHelper.newCondition("isdelete", SearchOperator.ne, "Y"));
            searchable.setPage(0, Integer.MAX_VALUE);
            Page<CustomerAddress> addresses = customerAddressService.queryCustomerAddress(command);
            
            List<CustomerAddress> list = addresses.content;
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", list);
            responseMsg.setData(data);
            return SUCCESS;
        }
    }

    private boolean isNeedSwapLanguage(String productCode) {
        WinitProductQueryCommand prodocutQComand = new WinitProductQueryCommand();
        prodocutQComand.setCode(productCode);
        WinitProduct winitProduct = winitProductService.queryProduct(prodocutQComand);
        if (winitProduct != null) {
            WinitRuleQueryCommand winitRuleQ = new WinitRuleQueryCommand();
            winitRuleQ.setWinitProductId(winitProduct.getWinitProductId());
            winitRuleQ.setRuleName(ApiConstant.RULE_NAME_LANG);
            winitRuleQ.setRuleAttribute(ApiConstant.RULE_ATTRIBUTE_SHIPPER_ADDR);
            WinitRule winitRule = winitRuleService.getWinitRuleByProductIdAndName(winitRuleQ);
            if (winitRule != null && winitRule.getRuleValue() != null
                && ApiConstant.LANGUAGE_EN_US.indexOf(winitRule.getRuleValue()) > -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 按地址ID与pickup服务和发货方式获得可提货的仓库接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     * @author peipei.zeng
     */
    @RequestMapping(value = "/getwarehouse")
    public String getwarehouse(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        JSONObject json = (JSONObject) requestMsg.getData();
        long addressId = json.getLong("addressID");
        String productCode = json.getString("winitProductCode");
        String dispatchType = json.getString("dispatchType");

        QueryWarehouseCommand command = new QueryWarehouseCommand();
        command.setCtx(CommandContext.getContext());
        command.setAddressId(addressId);
        command.setProductCode(productCode);
        command.setDispatchType(dispatchType);

        List<CustomerWarehouse> list = serviceWarehouseService.selectWarehouseByServiceAndAddress(command);

        // 根据产品获取仓库邮编列表
        Set<String> postSet = getWarehousePostList(productCode);

        List<CustomerWarehouse> rslist = new ArrayList<CustomerWarehouse>();

        // 根据仓库Code获取仓库Id
        if (list != null && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                if (StringUtils.isNotBlank(list.get(i).getWarehouseCode())) {
                    WarehouseCommand warehouseCommand = new WarehouseCommand();
                    warehouseCommand.setValue(list.get(i).getWarehouseCode());
                    List<Warehouse> warehouses = warehouseService.getWarehouseList(warehouseCommand);
                    if (warehouses != null && !warehouses.isEmpty() && postSet.contains(warehouses.get(0).getPostal())) {// 根据产品上配置的仓库过滤
                        CustomerWarehouse customerWarehouse = list.get(i);
                        customerWarehouse.setWarehouseId(warehouses.get(0).getWarehouseId());
                        rslist.add(customerWarehouse);
                    }
                }
            }
        }

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", rslist);
        responseMsg.setData(data);
        return SUCCESS;
    }

    private Set<String> getWarehousePostList(String winitProductCode) {
        List<Long> logisticsIdList = new ArrayList<Long>();
        WinitProductQueryCommand prodocutQComand = new WinitProductQueryCommand();
        prodocutQComand.setCode(winitProductCode);
        WinitProduct winitProduct = winitProductService.queryProduct(prodocutQComand);
        if (winitProduct != null) {
            LogisticsDistFindCommand command = new LogisticsDistFindCommand();
            Searchable<?> searchable = Searchable.newSearchable();
            searchable.and(SearchFilterHelper.newCondition("DISTRICT_TYPE",
                SearchOperator.eq,
                ApiConstant.LOGISTIC_TYPE_WAREHOUSE));
            searchable.and(SearchFilterHelper.newCondition("WINIT_PRODUCT_ID",
                SearchOperator.eq,
                winitProduct.getWinitProductId()));
            // 查询所有的值
            searchable.setPage(0, Integer.MAX_VALUE);
            command.setSearchable(searchable);
            Page<LogisticsDist> page = logisticsdistService.findLogisticsDist(command);
            if (null != page && null != page.getContent() && page.getContent().size() > 0) {
                for (LogisticsDist logisticsDist : page.getContent()) {
                    if (isActiveWinitProductSla(winitProduct.getWinitProductId(),
                        logisticsDist.getWinitLogisticsdistId())) {
                        logisticsIdList.add(logisticsDist.getWinitLogisticsdistId());
                    }
                }
            }
        }
        return getPostal(logisticsIdList);
    }

    /**
     * 获取物流分区的邮编
     * 
     * @param logisticList
     * @return
     */
    private Set<String> getPostal(List<Long> logisticList) {
        Set<String> postSet = new HashSet<String>();
        if (CollectionUtils.isNotEmpty(logisticList)) {
            for (Long logisticId : logisticList) {
                WinitPostFindCommand command = new WinitPostFindCommand();
                Searchable<?> searchable = Searchable.newSearchable();
                searchable.and(SearchFilterHelper.newCondition("WINIT_LOGISTICSDIST_ID", SearchOperator.eq, logisticId));
                // 查询所有的值
                searchable.setPage(0, Integer.MAX_VALUE);
                command.setSearchable(searchable);
                Page<WinitPost> page = winitPostService.findWinitPost(command);
                if (null == page || null == page.getContent() || page.getContent().size() < 1) {
                    continue;
                }
                for (WinitPost post : page.getContent()) {
                    if (!postSet.contains(post.getCode())) {
                        postSet.add(post.getCode());
                    }
                }
            }
        }
        return postSet;
    }

    private boolean isActiveWinitProductSla(long winitProductId, long logisticsDistId) {
        WinitSlaFindCommand command = new WinitSlaFindCommand();
        Searchable<?> searchable = Searchable.newSearchable();
        Date date = new Date();
        searchable.and(SearchFilterHelper.newCondition("EFFECTIVE", SearchOperator.lt, date));
        searchable.and(SearchFilterHelper.newCondition("EXPIRY", SearchOperator.gt, date));
        searchable.and(SearchFilterHelper.newCondition("WINIT_PRODUCT_ID", SearchOperator.eq, winitProductId));
        searchable.and(SearchFilterHelper.newCondition("SLA_BEGIAN", SearchOperator.eq, logisticsDistId));
        // 查询所有的值
        searchable.setPage(0, Integer.MAX_VALUE);
        command.setSearchable(searchable);
        Page<WinitSla> page = winitSlaService.findWinitSla(command);
        if (null != page && null != page.getContent() && page.getContent().size() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 按地址ID获取服务和仓库接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     * @author zibin.wang
     */
    @RequestMapping(value = "/getServiceAndWarehouse")
    public String getServiceAndWarehouse(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        JSONObject json = (JSONObject) requestMsg.getData();
        Long addressID = json.getLong("addressID");

        QueryServiceAndWarehouseCommand command = JSON.parseObject(requestMsg.getData().toString(),
            QueryServiceAndWarehouseCommand.class);
        command.setCtx(CommandContext.getContext());
        command.setAddressID(addressID);
        List<ServiceAndWarehouse> list = serviceWarehouseService.getServiceAndWarehouse(command);
        List<ServiceAndWarehouse> disList = new ArrayList<ServiceAndWarehouse>();
        Map<String, ServiceAndWarehouse> map = new HashMap<String, ServiceAndWarehouse>();
        for (ServiceAndWarehouse sw : list) {
            if (map.get(sw.getServiceCode()) == null) {
                map.put(sw.getServiceCode(), sw);
            } else {
                if (sw.getStatus().equals("APP")) {
                    map.put(sw.getServiceCode(), sw);
                } else if (!map.get(sw.getServiceCode()).getStatus().equals("APP") && sw.getStatus().equals("WFC")) {
                    map.put(sw.getServiceCode(), sw);
                }
            }
        }
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            disList.add(map.get(it.next()));
        }
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", disList);
        responseMsg.setData(data);
        return SUCCESS;
    }

    /**
     * 设置默认地址
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     * @author zibin.wang
     */
    @RequestMapping(value = "/setDefaultAddress")
    public String setDefaultAddress(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        JSONObject json = (JSONObject) requestMsg.getData();
        Long addressID = json.getLong("id");
        SetDefaultAddressCommand command = JSON.parseObject(requestMsg.getData().toString(),
            SetDefaultAddressCommand.class);
        command.setCtx(CommandContext.getContext());
        command.setId(addressID);
        customerAddressService.setDefaultAddress(command);
        return SUCCESS;
    }

    /**
     * 校验参数
     * 
     * @param json
     */
    private void validateParams(JSONObject json) {
        ValidateUtil.validMaxLength(json, "code", 8, true);
        ValidateUtil.validNotNull(json, "countryCode");
        String countryCode = json.getString("countryCode");
        if ("CN".equals(countryCode) || "HK".equals(countryCode) || "TW".equals(countryCode)) {
            ValidateUtil.validMaxLength(json, "provinceCode", 100, true);
            ValidateUtil.validMaxLength(json, "cnAddressDetail", 100, true);
            ValidateUtil.validMaxLength(json, "enAddressDetail", 100, true);

        } else {
            ValidateUtil.validMaxLength(json, "State", 100, true);
            ValidateUtil.validMaxLength(json, "doorplate", 100, false);
            ValidateUtil.validMaxLength(json, "addressDetail1", 100, true);
            ValidateUtil.validMaxLength(json, "addressDetail2", 100, true);

        }

        ValidateUtil.validMaxLength(json, "cityCode", 100, true);
        ValidateUtil.validMaxLength(json, "postCode", 100, true);
        ValidateUtil.validMaxLength(json, "contactPerson", 100, false);
        ValidateUtil.validMaxLength(json, "contactNumber", 100, false);
        ValidateUtil.validEmail(json, "email");
    }

    /**
     * 获取退货地址
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     * @author zibin.wang
     */
    @RequestMapping(value = "/queryReturnAddresses")
    public String queryReturnAddresses(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QueryReturnAddressesCommand command = JSON.parseObject(requestMsg.getData().toString(),
            QueryReturnAddressesCommand.class);
        command.setCtx(CommandContext.getContext());
        QueryReturnAddressesVo queryReturnAddressesVo = customerAddressService.queryReturnAddresses(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("queryReturnAddressesVo", queryReturnAddressesVo);
        responseMsg.setData(data);
        return SUCCESS;
    }

    /**
     * 获取仓库地址
     * 
     * @param requestMsg
     * @param responseMsg
     * @return String
     * @author zibin.wang
     */
    @RequestMapping(value = "/queryWarehouseAddresses")
    public String queryWarehouseAddresses(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QueryReturnAddressesCommand command = JSON.parseObject(requestMsg.getData().toString(),
            QueryReturnAddressesCommand.class);
        command.setCtx(CommandContext.getContext());

        QueryReturnAddressesVo queryReturnAddressesVo = customerAddressService.queryReturnAddresses(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", queryReturnAddressesVo.getWarehouseVos());
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    @RequestMapping(value = "/getAddressesReceiveDetails")
    public String getAddressesReceiveDetails(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	QueryReceiveAddressVerifyDetailCommand command = JSON.parseObject(requestMsg.getData().toString(),
    			QueryReceiveAddressVerifyDetailCommand.class);
        command.setCtx(CommandContext.getContext());
        List<ReceiveAddressVerifyVo> list = receiveAddressService.queryReceiveAddressVerifyDetail(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", list);
        responseMsg.setData(data);
        return SUCCESS;
    } 
    
    @RequestMapping(value = "/getAddressesByUserId")
    public String getAddressesByUserId(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        CustomerAddressQueryCommand command = JSON.parseObject(requestMsg.getData().toString(),
            CustomerAddressQueryCommand.class);
        command.setCtx(CommandContext.getContext());
        command.setUserId(Long.parseLong(CommandContext.getContext().getUserId()));
        List<CustomerAddress> list = customerAddressService.getCustomerAddressByUserId(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", list);
        responseMsg.setData(data);
        return SUCCESS;
    } 
    
}
